﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Money.Interfaces;
using Money.Data;
using One.Framework;
using Money.Entities;

namespace Money.Services
{
    public class RoleService : IRoleService
    {
        #region IRoleService Members

        private MoneyManagerDataContext context;

        public RoleService()
        {
            context = new MoneyManagerDataContext();
        }

        public void CreateRole(Role item)
        {
            try
            {
                context.Roles.InsertOnSubmit(item);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot create role");
            }
        }

        public void DeleteRole(int id)
        {
            try
            {
                Role item = new Role();
                item.RoleId = id;
                context.Roles.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot delete role");
            }
        }

        public void UpdateRole(Role item)
        {
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot update user");
            }
        }

        public Role GetRoleById(int id)
        {
            try
            {
                return context.Roles.Where(r => r.RoleId == id).SingleOrDefault<Role>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get role");
            }
        }

        public List<Role> GetRoles()
        {
            try
            {
                return context.Roles.ToList<Role>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get roles");
            }
        }

        #endregion
    }
}
